package yangwenbo;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
import java.util.Scanner;
import java.util.Set;

public class DoubleBall {
    public static int[] generateWinningNumbers() {
        Random random = new Random();
        Set<Integer> redSet = new HashSet<>();
        while (redSet.size() < 6) {
            redSet.add(random.nextInt(33) + 1);
        }
        ArrayList<Integer> redList = new ArrayList<>(redSet);
        int blue = random.nextInt(16) + 1;
        int[] result = new int[7];
        for (int i = 0; i < 6; i++) {
            result[i] = redList.get(i);
        }
        result[6] = blue;
        return result;
    }

    public static int[] getBetNumbers() {
        Scanner scanner = new Scanner(System.in);
        Set<Integer> redSet = new HashSet<>();
        int[] betNumbers = new int[7];
        System.out.println("请输入6个红色球号码（1 - 33）：");
        for (int i = 0; i < 6; i++) {
            while (true) {
                int num = scanner.nextInt();
                if (num >= 1 && num <= 33 &&!redSet.contains(num)) {
                    redSet.add(num);
                    betNumbers[i] = num;
                    break;
                } else {
                    System.out.println("输入无效，请重新输入：");
                }
            }
        }
        System.out.println("请输入1个蓝色球号码（1 - 16）：");
        while (true) {
            int num = scanner.nextInt();
            if (num >= 1 && num <= 16) {
                betNumbers[6] = num;
                break;
            } else {
                System.out.println("输入无效，请重新输入：");
            }
        }
        return betNumbers;
    }

    public static void checkWinning(int[] winning, int[] bet) {
        int redMatch = 0;
        int blueMatch = 0;
        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 6; j++) {
                if (winning[i] == bet[j]) {
                    redMatch++;
                }
            }
        }
        if (winning[6] == bet[6]) {
            blueMatch = 1;
        }
        System.out.println("中奖号码为：");
        printArray(winning);
        System.out.println("您投注号码为：");
        printArray(bet);
        System.out.printf("您命中了%d个红色球号码，%d个蓝色球号码。\n", redMatch, blueMatch);
        String prizeLevel = "";
        if (redMatch == 6 && blueMatch == 1) {
            prizeLevel = "一等奖";
        } else if (redMatch == 6 && blueMatch == 0) {
            prizeLevel = "二等奖";
        } else if (redMatch == 5 && blueMatch == 1) {
            prizeLevel = "三等奖";
        } else if (redMatch == 5 || (redMatch == 4 && blueMatch == 1)) {
            prizeLevel = "四等奖";
        } else if (redMatch == 4 || (redMatch == 3 && blueMatch == 1)) {
            prizeLevel = "五等奖";
        } else if ((redMatch == 2 && blueMatch == 1) || (redMatch == 1 && blueMatch == 1) || (redMatch == 0 && blueMatch == 1)) {
            prizeLevel = "六等奖";
        } else {
            prizeLevel = "未中奖";
        }
        System.out.println("根据规则，您" + prizeLevel + "！");
    }

    public static void printArray(int[] arr) {
        for (int num : arr) {
            System.out.print(num + " ");
        }
        System.out.println();
    }

    public static void main(String[] args) {
        int[] winningNumbers = generateWinningNumbers();
        int[] betNumbers = getBetNumbers();
        checkWinning(winningNumbers, betNumbers);
    }
}